package com.k.camera2

import android.hardware.Camera
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


class SFActivity : AppCompatActivity() {
    private val TAG = "SFActivity"
    lateinit var textView: TextView
    lateinit var surfaceView1: SurfaceView
    lateinit var surfaceView2: SurfaceView
    private var camera1: Camera? = null
    private var camera2: Camera? = null


    private var surfaceCallback1: SurfaceHolder.Callback = object : SurfaceHolder.Callback {
        // Surface创建时
        override fun surfaceCreated(holder: SurfaceHolder) {
            openCamera1(holder) // 开启相机
        }

        // Surface改变时
        override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        }

        // Surface销毁时
        override fun surfaceDestroyed(holder: SurfaceHolder) {
        }
    }

    private var surfaceCallback2: SurfaceHolder.Callback = object : SurfaceHolder.Callback {
        // Surface创建时
        override fun surfaceCreated(holder: SurfaceHolder) {
            openCamera2(holder) // 开启相机
        }

        // Surface改变时
        override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        }

        // Surface销毁时
        override fun surfaceDestroyed(holder: SurfaceHolder) {
        }


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_sfactivity)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initView()
    }

    private fun initView() {
        textView = findViewById(R.id.tv_test)
        textView.setScaleX(-1f)

        surfaceView1 = findViewById(R.id.sv1)
        surfaceView1.holder.addCallback(surfaceCallback1)

        surfaceView2 = findViewById(R.id.sv2)
        surfaceView2.holder.addCallback(surfaceCallback2)
    }

    /**
     * 打开相机1
     */
    private fun openCamera1(holder: SurfaceHolder) {
        try {
            camera1 = Camera.open(0) // 开启相机
            startPreview1(holder) // 设置预览
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 打开相机2
     */
    private fun openCamera2(holder: SurfaceHolder) {
        try {
            camera2 = Camera.open(1) // 开启相机
            startPreview2(holder) // 设置预览
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置预览窗口
     */
    private fun startPreview1(holder: SurfaceHolder) {
        setPreviewSize1() // 设置预览窗口尺寸
        camera1?.let {
            it.setPreviewDisplay(holder)
//            it.setDisplayOrientation(90) // 旋转角度
            it.startPreview() // 开启预览
        }
    }

    /**
     * 设置预览窗口
     */
    private fun startPreview2(holder: SurfaceHolder) {
        setPreviewSize2() // 设置预览窗口尺寸
        camera2?.let {
            it.setPreviewDisplay(holder)
//            it.setDisplayOrientation(90) // 旋转角度
            it.startPreview() // 开启预览
        }
    }

    /**
     * 设置预览窗口尺寸
     */
    private fun setPreviewSize1() {
        val supportedPreviewSizes: List<Camera.Size> = camera1!!.parameters.supportedPreviewSizes
        supportedPreviewSizes.forEach {
            Log.e(TAG, "支持的尺寸：${it.width}*${it.height}")
        }

        val parameters = camera1?.parameters
        val size = supportedPreviewSizes[0]
        parameters?.setPreviewSize(size.width, size.height)
        //  parameters?.horizontalViewAngle = 180f
        camera1?.parameters = parameters
    }


    /**
     * 设置预览窗口尺寸
     */
    private fun setPreviewSize2() {
        val supportedPreviewSizes: List<Camera.Size> = camera2!!.parameters.supportedPreviewSizes
        supportedPreviewSizes.forEach {
            Log.e(TAG, "支持的尺寸：${it.width}*${it.height}")
        }

        val parameters = camera2?.parameters
        val size = supportedPreviewSizes[0]
//        Log.e(TAG, "horizontalViewAngle${parameters?.horizontalViewAngle}", )
        parameters?.setPreviewSize(size.width, size.height)
        camera2?.parameters = parameters
    }

    override fun onStop() {
        super.onStop()
        camera1?.apply {
            stopLockTask()
            release()
        }
        camera1 = null

        camera2?.apply {
            stopLockTask()
            release()
        }
        camera2 = null
    }
}